using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace weather
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Radar : System.Windows.Forms.Form
	{
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.MainMenu mainMenu1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private static string radarid = "den";
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem5;
		private static string currentzip = "80302";
		private string imagesize = "large";

		public Radar()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			GetDoppler();
			pictureBox1.Size = pictureBox1.Image.Size;
			this.Size = pictureBox1.Size;
			this.Height += 40;
			this.Width += 25;

			this.Show();
			this.Activate();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Radar));
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.SuspendLayout();
			// 
			// pictureBox1
			// 
			this.pictureBox1.Location = new System.Drawing.Point(8, 8);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(480, 336);
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem3});
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 0;
			this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem4,
																					  this.menuItem5});
			this.menuItem3.Text = "Size";
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 0;
			this.menuItem4.Text = "Small";
			this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Checked = true;
			this.menuItem5.Index = 1;
			this.menuItem5.Text = "Large";
			this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
			// 
			// Radar
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(544, 373);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.pictureBox1});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.mainMenu1;
			this.Name = "Radar";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Doppler";
			this.Closed += new System.EventHandler(this.Radar_Close);
			this.ResumeLayout(false);

		}
		#endregion

		public void GetDoppler() 
		{

			try
			{
				string url =  "http://maps.weather.com/web/radar/us_"+radarid+"_closeradar_"+imagesize+"_usen.jpg";
				HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);

				HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();

				Stream str = ws.GetResponseStream();
				if( pictureBox1.Image != null )
					pictureBox1.Image.Dispose();
				pictureBox1.Image = Image.FromStream(str);
				byte[] inBuf = new byte[100000];

				int bytesToRead = (int) inBuf.Length;
				int bytesRead = 0;
				while (bytesToRead > 0) 
				{
					int n = str.Read(inBuf, bytesRead,bytesToRead);
					if (n==0)
						break;
					bytesRead += n;
					bytesToRead -= n;
				}				
				this.Text = "Doppler for: " + currentzip;
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex.ToString());
			}
		}

		
		private void menuItem4_Click(object sender, System.EventArgs e) 
		{
			menuItem4.Checked = true;
			menuItem5.Checked = false;
			imagesize = "small";
			GetDoppler();
			pictureBox1.Size = pictureBox1.Image.Size;
			this.Size = pictureBox1.Size;
			this.Height += 40;
			this.Width += 25;
		}

		private void menuItem5_Click(object sender, System.EventArgs e) 
		{
			menuItem4.Checked = false;
			menuItem5.Checked = true;
			imagesize = "large";
			GetDoppler();
			pictureBox1.Size = pictureBox1.Image.Size;
			this.Size = pictureBox1.Size;
			this.Height += 40;
			this.Width += 25;
		}

		private void Radar_Close(object sender, System.EventArgs e)
		{
			this.Dispose();
		}

	}
}
